package com.alianzamedica.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import com.alianzamedica.models.TagModel;

/**
 * @author Carlos
 * 
 */
public class TagServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1615906111187028892L;

	/**
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		try {
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder docBuilder = factory.newDocumentBuilder();
				Document doc = docBuilder.newDocument();
				Element root = doc.createElement("xml");
				doc.appendChild(root);
				String tag = request.getParameter("tag");
				tag = tag.replaceAll("0x", "");
				System.out.println("tag: " + tag);
				TagModel tModel = new TagModel();
				if (tag != null) {
					Element messageEl = doc.createElement("message");
					root.appendChild(messageEl);
					if (tModel.exists(tag) && !tModel.isUsed(tag)) {
						Text text = doc
								.createTextNode("el tag seleccionado esta disponible");
						messageEl.appendChild(text);
					} else {
						Text text = doc
								.createTextNode("el tag seleccionado no esta disponible");
						messageEl.appendChild(text);
					}
				}
				this.writeDocument(out, doc);
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		} finally {
			try {
				out.close();
			} catch (Exception e) {
			}
		}

	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);

	}

	/**
	 * @param out
	 * @param document
	 * @throws TransformerException
	 */
	public void writeDocument(PrintWriter out, Document document)
			throws TransformerException {
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer();
		Source source = new DOMSource(document);
		Result result = new StreamResult(out);
		transformer.transform(source, result);
	}

}
